package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.entity.User;
import com.dayuanit.dy15.atm.entity.UserDetail;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.mapper.UserDetailMapper;
import com.dayuanit.dy15.atm.mapper.UserMapper;
import com.dayuanit.dy15.atm.service.UserService;
import com.dayuanit.dy15.atm.util.MD5Utils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Value("${user.password.salt}")
    private String passwordSalt;

    @Value("${salt.value}")
    private String saltValue;

    public UserServiceImpl() {
        System.out.println("UserServiceImpl()");
    }

    @Override
    public User login(String username, String password) {
        if (StringUtils.isBlank(username) ||
                StringUtils.isBlank(password)) {
            throw new BizException("有必填参数为空");
        }

        // 用户存在吗？ 密码对吗？
        User user = userMapper.getUserByName(username);
        if (null == user) {
            throw new BizException("用户名不存在或者密码错误");
        }

        //密码的明文=密码 + 盐值 + 用户名  (要跟注册的时候明文规则一致)
        String passwordSign = MD5Utils.passwordSigin(password, passwordSalt, user.getUsername());
        if (!user.getPassword().equals(passwordSign)) {
            throw new BizException("用户名不存在或者密码错误");
        }

        return user;
    }

    @Override
    public void register(String username, String password, String confirmPassword) {
        if (StringUtils.isBlank(username) ||
                StringUtils.isBlank(password) ||
                StringUtils.isBlank(confirmPassword)) {
            //国际化的配置文件
            throw new BizException("有必填参数为空");
        }

        if (!confirmPassword.equals(password)) {
            throw new BizException("两次密码不相等");
        }

        // 判断用户名是否存在
        User user = userMapper.getUserByName(username);
        if (null != user) {
            throw new BizException("用户名已存在");
        }

        user = new User();
        user.setCreateTime(new Date());
        user.setUsername(username);

        //密码不能直接保存明文 要进行加密 一般采用消息摘要算法(MD5/SHA)
        String passwordSign = MD5Utils.passwordSigin(password, passwordSalt, user.getUsername());
        user.setPassword(passwordSign);

        int rows = userMapper.insert(user);
        if (1 != rows) {
            throw new BizException("注册失败");
        }
    }

    @Override
    public void resetPassword(String oldPassword, String newPassword, String confirmPassword, long userId) {
        if (StringUtils.isBlank(oldPassword) ||
                StringUtils.isBlank(newPassword) ||
                StringUtils.isBlank(confirmPassword)) {
            throw new BizException("有必填参数为空");
        }

        if (!newPassword.equals(confirmPassword)) {
            throw new BizException("两次输入的密码不相等");
        }

        // 原始密码是否相等
        User user = userMapper.selectByPrimaryKey(userId);
        if (null == user) {
            throw new BizException("用户不存在");
        }

        String passwordSign = MD5Utils.passwordSigin(oldPassword, passwordSalt, user.getUsername());
        if (!user.getPassword().equals(passwordSign)) {
            throw new BizException("原始密码错误");
        }

        //修改密码
        user.setPassword(MD5Utils.passwordSigin(newPassword, passwordSalt, user.getUsername()));
        int rows = userMapper.updateByPrimaryKey(user);
        if (1 != rows) {
            throw new BizException("修改密码失败");
        }

    }


    @Override
    public void retrievePwd(String email, String sign, long expireTime, String password, String confirmPwd) {
        // 明文的组成=email + 随机的字符串 + 过期时间(毫秒数)
        String msg = email + saltValue + expireTime;
        String pwdSign = DigestUtils.md5Hex(msg);

        if (System.currentTimeMillis() > expireTime) {
            throw new BizException("请求已过期");
        }

        if (!sign.equals(pwdSign)) {
            throw new BizException("签名错误");
        }

        if (!password.equals(confirmPwd)) {
            throw new BizException("两次密码不相等");
        }

        UserDetail userDetail = userDetailMapper.getByEmail(email);
        Long userId = userDetail.getUserId();
        User user = userMapper.selectByPrimaryKey(userId);

        user.setPassword(MD5Utils.passwordSigin(password, passwordSalt, user.getUsername()));
        int rows = userMapper.updateByPrimaryKey(user);
        if (1 != rows) {
            throw new BizException("找回密码失败");
        }

    }
}
